package com.swpu.eduservice.controller;


import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.eduservice.Client.OrderClient;
import com.swpu.eduservice.entity.Chapter.ChapterVo;
import com.swpu.eduservice.entity.EduCourse;
import com.swpu.eduservice.entity.vo.CourseInfoForm;
import com.swpu.eduservice.entity.vo.CoursePublishVo;
import com.swpu.eduservice.entity.vo.CourseQuery;
import com.swpu.eduservice.entity.vo.CourseWebVo;
import com.swpu.eduservice.service.EduChapterService;
import com.swpu.eduservice.service.EduCourseDescriptionService;
import com.swpu.eduservice.service.EduCourseService;
import com.swpu.eduservice.service.EduVideoService;
import com.swpu.servicebase.ExceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ygq
 * @since 2021-10-16
 */
@RestController
@RequestMapping("/eduservice/edu-course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService  eduCourseService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    //添加课程的基本信息的方法
    @PostMapping("/addCourseInfo")
    public R addCourse(@RequestBody CourseInfoForm courseInfoForm){
        String id =  eduCourseService.saveCourseInfo(courseInfoForm);
        return R.ok().data("courseId",id);
    }

    //根据课程id查询课程的基本信息
    @GetMapping("getCourseInfo/{id}")
    public R getInfo(@PathVariable String id){
        CourseInfoForm courseInfoForm =  eduCourseService.getCourseInfo(id);
        return R.ok().data("courseInfoVo",courseInfoForm);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R update(@RequestBody CourseInfoForm courseInfoForm){
        eduCourseService.updateCourse(courseInfoForm);
        return R.ok();
    }
    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public R getCoursePublishVoById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){

        CoursePublishVo courseInfoForm = eduCourseService.getCoursePublishVoById(id);
        return R.ok().data("publishCourse", courseInfoForm);
    }
    //课程的最终发布
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse  = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }
    //课程列表分页查询实现
    @GetMapping("/getPage/{pageId}/{limit}")
    public R getByPage(@PathVariable long pageId,
                       long limit){
        Page<EduCourse> page = new Page<>(pageId,limit);
        long total = page.getTotal();
        eduCourseService.page(page,null);
        List<EduCourse> list = page.getRecords();
        return R.ok().data("list",list).data("total",total);
    }
    //条件分页查询
    @PostMapping("/ConditionSearch/{PageId}/{limit}")
    public R ConditionSearch(@PathVariable long PageId,@PathVariable long limit,@RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> page = new Page<>(PageId,limit);


        QueryWrapper<EduCourse> wrapper  = new QueryWrapper<>();
        if(!StringUtils.isEmpty(courseQuery.getTitle())){
            wrapper.like("title",courseQuery.getTitle());
        }
        if(!StringUtils.isEmpty(courseQuery.getStatus())){
            wrapper.eq("status",courseQuery.getStatus());
        }
        if(!StringUtils.isEmpty(courseQuery.getBegin())){
            wrapper.ge("gmt_create",courseQuery.getBegin());
        }
        if(!StringUtils.isEmpty(courseQuery.getEnd())){
            wrapper.le("gmt_modified",courseQuery.getEnd());
        }
        eduCourseService.page(page,wrapper);
        List<EduCourse> list = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("list",list).data("total",total);
    }
    //删除课程
    @DeleteMapping("/deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        //删除小节
        eduVideoService.removeVedio(courseId);
        //删除章节
        eduChapterService.removeChapter(courseId);
        //删除描述
        eduCourseDescriptionService.removeById(courseId);
        //删除课程本身
        boolean id = eduCourseService.removeById(courseId);
        if(id) return  R.ok();
        else
        {
            throw new GuliException(20001,"删除失败");
        }
    }
    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId,HttpServletRequest request){

        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = eduCourseService.selectInfoWebById(courseId);

        //查询当前课程的章节信息
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCOurseId(courseId);
        //远程调用，判断课程是否被购买

        boolean buyCourse = orderClient.isBuyCourse(JwtUtils.getMemberIdByJwtToken(request), courseId);

        return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList).data("isbuy",buyCourse);
    }



}

